

package cbp

class ArticleController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ articleInstanceList: Article.list( params ), articleInstanceTotal: Article.count() ]
    }

    def show = {
        def articleInstance = Article.get( params.id )

        if(!articleInstance) {
            //flash.message = "Article not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ articleInstance : articleInstance ] }
    }

    def delete = {
		Article.withTransaction {
	        def articleInstance = Article.get( params.id )
	        if(articleInstance) {
	            try {
	                articleInstance.delete(flush:true)
	                //flash.message = "Article ${params.id} deleted"
	                redirect(action:list)
	            }
	            catch(org.springframework.dao.DataIntegrityViolationException e) {
	                //flash.message = "Article ${params.id} could not be deleted"
	                redirect(action:show,id:params.id)
	            }
	        }
	        else {
	            //flash.message = "Article not found with id ${params.id}"
	            redirect(action:list)
	        }			
		}
    }

    def edit = {
        def articleInstance = Article.get( params.id )

        if(!articleInstance) {
            //flash.message = "Article not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ articleInstance : articleInstance ]
        }
    }

    def update = {
		Article.withTransaction {
	        def articleInstance = Article.get( params.id )
	        if(articleInstance) {
	            if(params.version) {
	                def version = params.version.toLong()
	                if(articleInstance.version > version) {
	                    
	                    articleInstance.errors.rejectValue("version", "article.optimistic.locking.failure", "Another user has updated this Article while you were editing.")
	                    render(view:'edit',model:[articleInstance:articleInstance])
	                    return
	                }
	            }
	            articleInstance.properties = params
	            if(!articleInstance.hasErrors() && articleInstance.save()) {
	                //flash.message = "Article ${params.id} updated"
	                redirect(action:show,id:articleInstance.id)
	            }
	            else {
	                render(view:'edit',model:[articleInstance:articleInstance])
	            }
	        }
	        else {
	            //flash.message = "Article not found with id ${params.id}"
	            redirect(action:list)
	        }			
		}
    }

    def create = {
        def articleInstance = new Article()
        articleInstance.properties = params
        return ['articleInstance':articleInstance]
    }

    def save = {
        def articleInstance = new Article(params)
		Article.withTransaction {
	        if(articleInstance.save(flush:true)) {
	            //flash.message = "Article ${articleInstance.id} created"
	            redirect(action:show,id:articleInstance.id)
	        }
	        else {
	            render(view:'create',model:[articleInstance:articleInstance])
	        }
		}
    }
}
